<?php

namespace Laoha\Core;

use Illuminate\Routing\Controller;
use Encore\Admin\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets;
use Illuminate\Http\Request;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Artisan;

class SetController extends Controller
{
    public $name;
	public $data;
	public function index(Content $content)
    {
		return $content
            ->title('设置')
            ->body(Widgets\Tab::forms(				
				config('laoha.set_tabs')
			));
    }

    public function save(Request $request)
    {
		$name = $request->get('active');
		if(empty($name)){
			admin_error('错误', "未指定设置对象.");
			return back();		
		}
		$data = require(config_path('sets/'.$name.'.php'));
		$data = is_array($data) ? $data : [];
		$parameters = $request->except(['_token','_pjax','active']);
		if (!empty($parameters)) {						
			$disk  = config('admin.upload.disk');
			$disk_url  = config('filesystems.disks.'.$disk.'.url') ? config('filesystems.disks.'.$disk.'.url') : '';
			foreach($parameters as $k=>$v){
				if($v instanceof UploadedFile){
					$img = $v->store('images',$disk); //参数path，disk.root
					$parameters[$k] = $disk_url.'/'.$img;
				}
			}
			$set = array_merge($data,$parameters);
			$s="<?php return \n\t" . var_export($set, true) . ";";
			file_put_contents(config_path('sets/'.$name.'.php'), $s);
			//Artisan::call('config:clear');
		}
        admin_success('设置完成.查看正确结果请刷新');
		return back(200);
		//return redirect(admin_base_path('laoha/set').'?active='.$name,200);
    }
}